Skip to content

Implement reflecting boundary conditions for metadynamics bias#525

Draft
fabsugar wants to merge 63 commits into
masterfrom
meta_reflection
Draft

Implement reflecting boundary conditions for metadynamics bias#525
fabsugar wants to merge 63 commits into
masterfrom
meta_reflection

Conversation

@fabsugar

Copy link
Copy Markdown
Contributor

Dear All,
the branch meta_reflection includes important modifications of the metadynamics code (colvarbias_meta.*) which allow to correct known artifacts of metadynamics at the boundaries of the collective variables (either natural boundaries or added through flat bottom potentials).
Namely, I implemented the reflection of the metadynamics Gaussians through the boundaries and an additional feature that removes the biasing forces beyond the boundaries (so that those forces smoothly go to zero beyond the boundaries). The implementation is specifically designed to be general and applicable to any dimensionality. The output pmf and bias energy includes those corrections (e.g. it remains flat beyond the border of a given collective variable while keeping the same shape across the others).
This feature also solves the need to add explicit Gaussians beyond the boundaries, which is computationally inefficient (and currently might need some fix).
The new feature is simple to use, it only requires the keyword "hills_reflection on" and automatically works on the default boundaries. Upon activating it the users will be able to run long metadynamics simulations of any dimensionality without developing the large errors at the boundaries that are currently present and that become larger and larger as the simulation goes on.
The new feature is fully tested on different systems up to a dimensionality of four.
The meta_reflection branch was merging fine with the master branch up to mid January, right now there is some minor conflict.
All the best,
Fabrizio

@giacomofiorin

Copy link
Copy Markdown
Member

Looking at the state of the history a squash-merge (i.e. merging all commits into one commit) may be appropriate when this PR is ready.

The conflict is near the function where the hills are added in the multiple walkers case. Have you been able to test that combination of features before the conflicts became worse?

@giacomofiorin

Copy link
Copy Markdown
Member

P.S. I tried resolving the conflicts in the meta_reflection_squash branch.

@fabsugar

fabsugar commented Apr 18, 2023 via email

Copy link
Copy Markdown
Contributor Author

@giacomofiorin

Copy link
Copy Markdown
Member

Sounds good. You could also start from meta_reflection_squash

@fabsugar

fabsugar commented Apr 18, 2023 via email

Copy link
Copy Markdown
Contributor Author

@giacomofiorin
giacomofiorin force-pushed the meta_reflection branch 2 times, most recently from ffb677b to 6e7559f Compare May 3, 2023 13:37
@giacomofiorin giacomofiorin changed the title Meta reflection Implement reflecting boundary conditions for metadynamics bias Oct 24, 2024
fabsugar and others added 19 commits May 5, 2025 14:30
must be put back at the border when coordinates go out of the reflection border
…mit of reflection is in the boundary

before correction was handling most of cases but now should be fine in every case
by using get_colvar_index_bound to treat vaules out of the boundary (which are projected inside the closest boundary).
Additionally curr_values are now defined as a general variable to avoid define a new variable every time
functions are called
…xes the seg fault when using

multiple replicas
…r non scalar variables as reflection is focused on scalae CVs and has not been tested for a mix of scalar and vetor variables (but might work).
…should be set on automatically when using grids

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds reflective boundary corrections to metadynamics to reduce PMF artifacts and suppress biasing forces beyond configured boundaries.

Changes:

  • Implements multidimensional hill reflection and boundary-aware energy/force evaluation.
  • Extends grid projection and restart handling.
  • Adds documentation, citations, and a NAMD regression test.

Reviewed changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 15 comments.

Show a summary per file
File Description
src/colvargrid.h Adds bounded grid-index lookup.
src/colvarbias_meta.h Declares reflection configuration and state.
src/colvarbias_meta.cpp Implements reflection, clamping, and grid integration.
namd/tests/library/000_distance-grid_metadynamics-reflection/test.in Configures the reflection regression test.
namd/tests/library/000_distance-grid_metadynamics-reflection/namd-version.txt Records the tested NAMD version.
.../AutoDiff/test.restart.pmf Stores restarted PMF baseline.
.../AutoDiff/test.restart.colvars.traj Stores restarted trajectory baseline.
.../AutoDiff/test.restart.colvars.state.stripped Stores restarted state baseline.
.../AutoDiff/test.restart.colvars.out Stores restarted log baseline.
.../AutoDiff/test.pmf Stores initial PMF baseline.
.../AutoDiff/test.colvars.traj Stores initial trajectory baseline.
.../AutoDiff/test.colvars.state.stripped Stores initial state baseline.
.../AutoDiff/test.colvars.out Stores initial log baseline.
doc/colvars-refman.bib Adds the analysis-method citation.
doc/colvars-refman-main.tex Documents reflective boundary behavior and options.
doc/colvars-code-refs.bib Associates reflection with the Colvars reference.
Comments suppressed due to low confidence (1)

namd/tests/library/000_distance-grid_metadynamics-reflection/test.in:37

  • The PR description says users should enable this with hills_reflection on, but the parser, test, and reference manual expose only useHillsReflection. Update the PR description to name the implemented keyword (or add the promised alias) so users do not receive an unknown-keyword error.
    useHillsReflection on

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/colvarbias_meta.cpp
Comment on lines +1133 to +1135
curr_bin = hills_energy->get_colvars_index_bound(curr_values);
} else {
curr_bin = hills_energy->get_colvars_index(curr_values);
Comment thread src/colvarbias_meta.cpp
Comment on lines +1215 to 1219
if (use_reflection) {
curr_bin = hills_energy->get_colvars_index_bound(curr_values);
} else {
curr_bin = hills_energy->get_colvars_index(curr_values);
}
Comment thread src/colvarbias_meta.cpp
Comment on lines +490 to +495
if (get_keyval(conf, "reflectionUpLimit", reflection_ulimit, reflection_ulimit)) {
for (i = 0; i < nrefvarsu; i++) {
if (use_grids) {
icount=reflection_ulimit_cv[i];
cvm:: real bound=hills_energy->upper_boundaries[icount].real_value;
if (reflection_ulimit[i] != bound && reflection_ulimit[i] > bound-variables(icount)->width) {
hillWeight 0.001
hillWidth 1.2533141373155001 # Old default
newHillFrequency 10
useHillsReflection on
upperBoundary 10.2

# not required to define hard limits but recommended to avoid unnecessary grid expansion
# likely to be activated by default with refletion in the future
However, \texttt{upperBoundary} does not have such a ``natural'' choice of value.
The activation of the reflective boudary correction via \refkey{useHillsReflection}{metadynamics|useHillsReflection} set to \texttt{on}, will lead to hills that are reflected at distances of 0 and 13.

\emph{What is the impact of the reflective boudary correction onto the PMF?} Not a very complicated one: the PMF reconstructed by metadynamics will simply show a slight flattening very close to the boundaries (\texttt{r}~$=$ 0~\AA{} and \texttt{r}~$=$ 13~\AA{}), with the biasing forces becoming negligible. Those forces are completely removed after crossing the boundaries (\texttt{r}~$>$ 13~\AA{}) and the metadynamics bias potential remains constant. Thus, the only biasing forces applied beyond the upper boundary are those from the \texttt{harmonicWalls} restraints.
Comment thread src/colvarbias_meta.h
Comment on lines +159 to +160
/// \brief Check is current colvar value is within inversion or
/// reflection limits to assess whether to add a hill
Comment thread src/colvarbias_meta.h
std::vector<int> which_int_llimit_cv;
std::vector<int> which_int_ulimit_cv;

/// \brief Current value of colvars to be modifed for calculation of energy and forces with interval
Comment thread src/colvarbias_meta.cpp
cvm::log("Reflection condition upper limit for this CV is "+cvm::to_str(reflection_ulimit[i])+".\n");
}

// multimensional reflection
Comment thread src/colvarbias_meta.cpp
Comment on lines +744 to +748
// sum over all possible lower and upper boudary combinations
// exploiting kstate=ref_state[k][kcount]:
// for just one reflection these are 0(lower boundary) and 1(upper boundary)
// for two reflections these are 0 1 10 11 (0,0 0,1 1,0 1,1)
// where 0 is reflect on the two lower boudaries of the two coordinates etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants